#subroutines to trim whitespaces in a string variable
sub trim {
    my($str) = shift;
   
    $str =~ m!^\s*(.+?)\s*$!i;
    defined $str ? return $str : return '';
}

sub trimIfEmpty {
    my($str) = shift;
    if(!defined($str)) {
        $str = "NULL";
    } else {
        $str = trim($str);
    }
    return $str;
}

sub filterParuse {
    my $paruse = shift;
    if($paruse eq "NA") {
        $paruse = 1;
    } elsif($paruse == 10) {
        $paruse = "NULL";
    }
    
    return $paruse;
}


sub filterChorientA {
    my $paruse = shift;
    my $chorient = shift;
    my $chorientA;
    if($paruse eq "NULL" or $paruse == 5) {
      $chorientA = "NULL";  
    } elsif($paruse != 6){
        $chorientA = "N";
    } else{
        if($chorient eq "NA") {
            $chorientA = "NULL";
        } else {
            if(($chorient >= 11 and $chorient <= 19) or $chorient == 22) {
                $chorientA = "F";
            } elsif(($chorient >= 1 and $chorient <= 9) or $chorient == 21){
                $chorientA = "R";
            } elsif($chorient == 0 or $chorient == 29){
                $chorientA = "NULL";
            }
        }
    }
    
    return $chorientA;
}

sub filterSeatpos {
    my $seatpos = shift;
    my ($seatpos_row, $seatpos_col);
    
    #determining seatpos_row
    if($seatpos eq "NA") {
        $seatpos_row = "NULL";
    } elsif($seatpos >= 11 and $seatpos <= 19) {
        $seatpos_row = "Front";
    } elsif($seatpos >= 21 and $seatpos <= 29) {
        $seatpos_row = "Second";
    } elsif($seatpos >= 31 and $seatpos <= 39) {
        $seatpos_row = "Third";
    } elsif($seatpos >= 41 and $seatpos <= 49) {
        $seatpos_row = "Fourth";
    } else {
        $seatpos_row = "Other";
    }
    
    #determining seatpos_col
    if($seatpos eq "NA") {
        $seatpos_col = "NULL";
    } elsif($seatpos%10 == 9) {
        $seatpos_col = "NULL";
    } elsif($seatpos%10 == 1) {
        $seatpos_col = "Left";
    } elsif($seatpos%10 == 2) {
        $seatpos_col = "Middle";
    } elsif($seatpos%10 == 3) {
        $seatpos_col = "Right";
    } else {
        $seatpos_col = "Other";
    }
    
    return ($seatpos_row, $seatpos_col);
}

sub filterEjection {
    my $ejection = shift;
    if($ejection eq "NA") {
        $ejection = "NotApp";
    } else {
        if($ejection == 3) {
            $ejection = "NULL";
        }
    }
    
    return $ejection;
}

sub filterGlasgow {
    my $glasgow = shift;
    if($glasgow eq "NA" or $glasgow == 1 or $glasgow == 2 or $glasgow == 97) {
        $glasgow = "NULL";
    } elsif($glasgow == 0) {
        $glasgow = 15;
    }
    
    return $glasgow;
}

sub filterTreatmnt {
    my $treatmnt = shift;
    if($treatmnt eq "NA") {
        $treatmnt = "NULL";
    }
    
    return $treatmnt;
}

sub filterDeath {
    my $death = shift;
    if($death eq "NA") {
        $death = "NULL";
    } elsif($death == 0) {
        $death = 0;
    } else {
        $death = 1;
    }
    
    return $death;
}

sub filterAiscode {
    my $aiscode = shift;
    if($aiscode eq "NANANANA") {
        $aiscode = "NULL";
    }
    
    return $aiscode;
}

sub filterMancoll {
    my $mancoll = shift;
    if($mancoll eq "NA") {
        $mancoll = "NULL";
    }
    
    return $mancoll;
}

sub filterClass {
    my $class = shift;
    if($class eq "NA") {
        $class = "NULL";
    } elsif($class >= 1 and $class <= 9) {
        $class = "Car";
    } elsif($class >= 20 and $class <= 29) {
        $class = "Van";
    } elsif($class >= 30 and $class <= 39) {
        $class = "Truck";
    } elsif($class >= 14 and $class <= 19) {
        $class = "SUV";
    } else {
        $class = "Other";
    }
    
    return $class;
}

sub filterObjcont {
    my $objcont = shift;
    if($objcont >= 36 and $objcont <= 39) {
        $objcont = "Non-collision";
    } elsif(($objcont >= 1 and $objcont <= 11) or ($objcont >= 31 and $objcont <= 35) or ($objcont >= 72 and $objcont <= 89)) {
        $objcont = "Collision-person";
    } elsif($objcont >= 41 and $objcont <= 71) {
        $objcont = "Collision-object";
    } elsif($objcont == 98) {
        $objcont = "Other";
    }
    
    return $objcont;
}

sub filterDvtotal {
    my $dvtotal = shift;
    if($dvtotal eq "NA") {
        $dvtotal = "NULL";
    }
    
    return $dvtotal;
}

sub filterDvest {
    my $dvest = shift;
    if($dvest eq "NA" or $dvest == 0) {
        $dvest = "NULL";
    }
    
    return $dvest;
}

sub filterSplimit {
    my $splimit = shift;
    if($splimit eq "NA" or $splimit > 80){
        $splimit = "NULL";
    }
    
    return $splimit;
}

sub assignHKeys {
    my $hUID_fieldRef = shift;
    my $hFieldRef = shift;

    my %hUID_field = %$hUID_fieldRef;
    my %hField = %$hFieldRef;
    
    foreach my $i (sort keys %hUID_field) {
        if(exists($hField{$i})) {
            $hField{$i} = $hField{$i} + 1;
        } else {
            $hField{$i} = 1;
        }
    }
    
    return %hField;
}

#NTDB Procedures
sub filterGCS_NTDB_forCDS
{
    my ($edgcseye, $edgcsverb, $edgcsmotor, $edgcstotal);
    $edgcseye = shift; $edgcsverb = shift; $edgcsmotor = shift; $edgcstotal = shift;
    
    if($edgcseye ne "NULL"){
        if($edgcseye < 1 or $edgcseye > 4){
            $edgcseye = "NULL";
        }
    }
    if($edgcsverb ne "NULL"){
        if($edgcsverb < 1 or $edgcsverb > 5){
            $edgcsverb = "NULL";
        }
    }
    if($edgcsmotor ne "NULL"){
        if($edgcsmotor < 1 or $edgcsmotor > 6){
            $edgcsmotor = "NULL";
        }
    }
    if($edgcstotal ne "NULL"){
        if($edgcstotal < 3 or $edgcstotal > 15){
            $edgcstotal = "NULL";
        }
    }
    
    if($edgcseye eq "NULL" and $edgcsverb ne "NULL" and $edgcsmotor ne "NULL" and $edgcstotal ne "NULL"){
        $edgcseye = $edgcstotal - $edgcsverb - $edgcsmotor;
    }
    if($edgcseye ne "NULL" and $edgcsverb eq "NULL" and $edgcsmotor ne "NULL" and $edgcstotal ne "NULL"){
        $edgcsverb = $edgcstotal - $edgcseye - $edgcsmotor;
    }
    if($edgcseye ne "NULL" and $edgcsverb ne "NULL" and $edgcsmotor eq "NULL" and $edgcstotal ne "NULL"){
        $edgcsmotor = $edgcstotal - $edgcseye - $edgcsverb;
    }
    if($edgcseye ne "NULL" and $edgcsverb ne "NULL" and $edgcsmotor ne "NULL" and $edgcstotal eq "NULL"){
        $edgcstotal = $edgcseye + $edgcsverb + $edgcsmotor;
    }

    if($edgcseye ne "NULL"){
        if($edgcseye < 1 or $edgcseye > 4){
            $edgcseye = "NULL";
        }
    }
    if($edgcsverb ne "NULL"){
        if($edgcsverb < 1 or $edgcsverb > 5){
            $edgcsverb = "NULL";
        }
    }
    if($edgcsmotor ne "NULL"){
        if($edgcsmotor < 1 or $edgcsmotor > 6){
            $edgcsmotor = "NULL";
        }
    }
    if($edgcstotal ne "NULL"){
        if($edgcstotal < 3 or $edgcstotal > 15){
            $edgcstotal = "NULL";
        }
    }
    
    return ($edgcseye, $edgcsverb, $edgcsmotor, $edgcstotal);
}

sub filterStatus_NTDB_forCDS {
    my ($hospdisp, $eddisp, $output);
    $hospdisp = shift; $eddisp = shift;
    $output = 0;
    
    if($hospdisp eq "Expired" or $eddisp eq "Died") {
        $output = 1;
    }
   
    return $output;
}


1;